package com.fool
{
	import flash.net.SharedObject;
	
	import mx.collections.ArrayCollection;

	public class Configuration 
	{
		private static var _instance:Configuration;
		
		private var user:User;
		private var entyties:Object;
		private var so:SharedObject;
		
		public function Configuration(user:ISerializable, opponent:ISerializable, deck:ISerializable, table:ISerializable)
		{
			entyties = {
				'user': user,
				'opponent':opponent, 
				'table':table, 
				'deck':deck
			}
				
			this.user = user as User;
			
			so = SharedObject.getLocal("foolID");
		}
		

		public function get exist():Boolean
		{
			return so && so.data.game;
		}
		
		public function clear():void
		{
			return so.clear() ;
		}
		
		public function saveState():void
		{
			var object:Object = {};
			for (var key:String in entyties) 
			{
				var entity:ISerializable = 	entyties[key];
				var value:String = entity.serialize();
				trace(key,value)
				object[key] = value; 
			}
			
			object['userIsAttacking'] = user.isAttacking;
			
			so.data.game = object;
			so.flush();
		}
		
		public function restore():void
		{
			var object:Object = so.data.game;
			user.isAttacking = object['userIsAttacking'];
			delete  object['userIsAttacking'];
			
			for (var key:String in object)
			{
				var entity:ISerializable = 	entyties[key];
				entity.deSerialize( object[key] );
			}
			
			
		}
	}
}